// pages/manager/reserve/reserve.js
import Dialog from '@vant/weapp/dialog/dialog';
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color1: "#FF8D1A",
    color2: "white",
    orderInfo:[],
    statt:0
  },
  clickstatus:function(event) {
    var that = this
    let id = event.currentTarget.dataset.id
    // console.log(id)
    Dialog.confirm({
      title: '是否确认已取餐',
      message: '确认后不可更改',
    })
    .then(() => {
      wx.cloud.callFunction({
        name: 'lu',
        data: {
          action: 'orderupdate',
          id: id,
          order_status: "1"
        },
        success(res) {
          console.log("订单云函数修改数据成功",res)
          let statt = that.data.statt
          if(statt == 0) {
            that.goOrder()
          }else{
            that.goAllorder()
          }
        },
        fail(res) {
          console.log("函数获取数据失败",res)
        }
      })
    })
    .catch(() => {
      // on cancel
    });
    

  },
  goAllorder:function() {
    util.time()
    var that = this
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    that.setData({
      statt:1
    })
    wx.cloud.callFunction({
      name: 'lu',
      data: {
        action: 'orderinfo',
        canteen_id: canteen_id2,
        window_id: window_id2
      },
      success(res) {
        let meal = res.result.data
        that.setData({
          orderInfo: meal,
          color2: "#FF8D1A",
          color1: "white",
        })
        console.log("云函数获取全部数据成功",meal)
      },
      fail(res) {
        console.log("函数获取全部数据失败",res)
      }
    })
  },
  goOrder:function() {
    util.time()
    var that = this
    let canteen_id2 = wx.getStorageSync('canteen').canteen_id;
    let window_id2 = wx.getStorageSync('window_id');
    that.setData({
      statt:0
    })
    wx.cloud.callFunction({
      name: 'lu',
      data: {
        action: 'order',
        canteen_id: canteen_id2,
        window_id: window_id2
      },
      success(res) {
        let meal = res.result.data
        that.setData({
          orderInfo: meal,
          color1: "#FF8D1A",
          color2: "white",
        })
        console.log("云函数获取待取餐数据成功",meal)
      },
      fail(res) {
        console.log("函数获取待取餐数据失败",res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var status = options.status
    if(status==1) {
      that.goAllorder()
    }else {
      that.goOrder()
    }
    
  },

  onClickLeft() {
    wx.navigateBack()
  },
  onShow: function () {
    util.time()
  },
})